package sanrenxing.dayEnd.backRepertoryTotal.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sanrenxing.dayEnd.backRepertoryTotal.domain.CashStockLXF;
import sanrenxing.dayEnd.backRepertoryTotal.service.CashStockServiceLXF;


/**
 * @desrciption 现金库存  核心控制器
 * @auth：yidu 李小锋
 * @date:2021/3/21 0021 - 11:54
 * @version:1:0
 */
@RestController
@RequestMapping("/Cashcontroller")
public class CashStockControllerLXF {
    //自动装配
    @Autowired
    private CashStockServiceLXF equityDataService;
    /**
     * 新增方法
     * @param equityData 对象
     * @return 0,failure 1，SUCCESS
     */
    @PostMapping("/cashStock")
    public String add(@RequestBody CashStockLXF equityData){
        //获取添加的行数
        int rows = equityDataService.add(equityData);
        //使用三元运算符判断
        return rows>0?"add SUCCESS":"add failure";
    }
    /**
     * 删除方法
     * @param cashId 证券编号
     * @return 0,failure 1，SUCCESS
     */
    @DeleteMapping("/cashStock/{cashId}")
    //PathVariable：接收请求路径中占位符的值括号中可有可无但是必须一样
    public String delete(@PathVariable("cashId") int cashId){
        //获取删除的行数
        int rwos=equityDataService.delete(cashId);
        //使用三元运算符判断
        return rwos>0?"delete SUCCESS":"delete failure";
    }
    /**
     * 修改方法方法
     * @param equityData 对象
     * @return 0,failure 1，SUCCESS
     */
    @PutMapping("/cashStock")
    //RequestBody:表示前端传递的json格式转化为javaBean即domain
    //ResponseBody:表示后端传递的javaBean格式转化为json格式
    public String update(@RequestBody CashStockLXF equityData){
        //获取修改的行数
        int rwos=equityDataService.update(equityData);
        //使用三元运算符判断
        return rwos>0?"update SUCCESS":"update failure";
    }

    /**
     * 批量删除
     * @param cashIdStr 数据证券ID
     * @return 0,failure 1，SUCCESS
     */
    @PutMapping("/batchBel/{cashIdStr}")
    public String batchBeleteById(@PathVariable("cashIdStr") String cashIdStr) {
        //对员工编号字符串按逗号分隔
        String[] empNos = cashIdStr.split(",");
        //定义返回值
        String result = "delete success";
        //循环遍历出证券数据id
        for (String equityData : empNos) {
            //调用业务层删除
            int rows = equityDataService.delete(Integer.parseInt(equityData));
            //判断
            if (rows <= 0) {
                return "batch delete failure";
            }
        }
        return result;
    }
}
